import 'package:flutter/material.dart';
import 'package:live_bg_mobile/common/enum/btn_enum.dart';
import 'package:live_bg_mobile/common/util/common_util.dart';

import '../common/const/app_color_const.dart';
import '../common/const/app_style_const.dart';
import 'icon_outlined_btn.dart';

class SearchBar extends StatefulWidget {
  final EdgeInsets? padding;
  final Color? backgroundColor;
  final Widget? searchBtn;
  final double searchInputHeight;
  final Color? searchInputBg;
  final TextStyle? searchInputStyle;
  final TextEditingController? searchInputController;

  final double searchPrefixIconSize;
  final Color searchPrefixIconColor;
  final IconData? searchPrefixIcon;
  final Widget? searchPrefix;

  final double searchSuffixIconSize;
  final Color searchSuffixIconColor;
  final IconData? searchSuffixIcon;
  final Widget? searchSuffix;

  final String? hintText;
  final TextStyle? hintStyle;

  final BtnType btnType;
  final String? btnText;
  final Function(String value)? onBtnPressed;

  final ValueChanged<String>? onSearchChange;
  final ValueChanged<String>? valueChanged;

  const SearchBar(
      {Key? key,
      this.padding,
      this.btnType = BtnType.primary,
      this.backgroundColor = whiteColor,
      this.searchInputBg = lightGrayColor8,
      this.searchInputHeight = 36,
      this.searchInputStyle,
      this.searchPrefix,
      this.searchInputController,
      this.searchPrefixIconColor = blackColor66,
      this.searchPrefixIconSize = 20,
      this.searchPrefixIcon,
      this.searchSuffix,
      this.searchSuffixIconColor = blackColor66,
      this.searchSuffixIconSize = 20,
      this.searchSuffixIcon,
      this.searchBtn,
      this.hintText,
      this.btnText,
      this.onBtnPressed,
      this.onSearchChange,
      this.valueChanged,
      this.hintStyle})
      : super(key: key);

  @override
  SearchBarState createState() => SearchBarState();
}

class SearchBarState extends State<SearchBar> {
  bool searchSuffixIconVisible = false;
  TextEditingController textInputController = TextEditingController();

  @override
  void initState() {
    super.initState();

  }

  @override
  void dispose() {
    super.dispose();
    textInputController.dispose();
    CommonUtil.textInputHide();
  }

  Widget suffix() {
    if (null != widget.searchSuffix) {
      return widget.searchSuffix!;
    } else {
      return searchSuffixIconVisible
          ? InkWell(
              onTap: () {
                CommonUtil.textInputHide();
                if(null!=widget.valueChanged){
                  widget.valueChanged!('');
                }
                if (null != widget.searchInputController) {
                  widget.searchInputController!.clear();
                } else {
                  textInputController.clear();
                  setState(() {
                    searchSuffixIconVisible = false;
                  });
                }
              },
              child: SizedBox(
                width: widget.searchInputHeight,
                height: widget.searchInputHeight,
                child: Icon(widget.searchSuffixIcon ?? Icons.cancel_outlined,
                    size: widget.searchSuffixIconSize,
                    color: widget.searchSuffixIconColor),
              ))
          : Container();
    }
  }

  Widget searchBtn() {
    return widget.searchBtn ??
        Padding(
          padding: const EdgeInsets.only(left: 8),
          child: IconOutlinedBtn(
            text: widget.btnText ?? '搜索',
            btnType: widget.btnType,
            onPressed: () {
              CommonUtil.textInputHide();
              if (null != widget.onBtnPressed) {
                String value = null != widget.searchInputController
                    ? widget.searchInputController!.text
                    : textInputController.text;
                CommonUtil.debounce(() {
                  widget.onBtnPressed!(value);
                }, null)();
              }
            },
          ),
        );
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.backgroundColor,
      padding: widget.padding ??
          const EdgeInsets.only(left: 10, right: 10, top: 6, bottom: 6),
      child: Row(
        children: [
          Expanded(
              child: Container(
            color: widget.searchInputBg,
            child: Row(
              children: [
                widget.searchPrefix ??
                    SizedBox(
                      width: widget.searchInputHeight,
                      height: widget.searchInputHeight,
                      child: Icon(widget.searchPrefixIcon ?? Icons.search,
                          size: widget.searchPrefixIconSize,
                          color: widget.searchPrefixIconColor),
                    ),
                Expanded(
                  child: TextField(
                      controller:
                          widget.searchInputController ?? textInputController,
                      decoration: InputDecoration(
                        isDense: true,
                        border: InputBorder.none,
                        hintText: widget.hintText ?? '请输入关键字',
                        hintStyle: widget.hintStyle ?? searchBarHintStyle,
                      ),
                      style: widget.searchInputStyle ?? searchBarInputStyle,
                      textInputAction: TextInputAction.search,
                      onChanged: widget.onSearchChange ??
                          (value) {
                            if (value.isNotEmpty) {
                              setState(() {
                                searchSuffixIconVisible = true;
                              });
                            } else {
                              setState(() {
                                searchSuffixIconVisible = false;
                              });
                            }
                            if(null!=widget.valueChanged){
                              widget.valueChanged!(value);
                            }
                          }),
                ),
                suffix(),
              ],
            ),
          )),
          searchBtn()
        ],
      ),
    );
  }
}
